﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GameLib.Utils;
using Sandbox.ContentTypes;
using Sandbox.ContentCollections;
using Sandbox.Main;
using Sandbox;
using GameLib;
using Sandbox.Scene;
using GameLib.Main;
using Sandbox.Editor.Modes;
using GameLib.WorldMap;
using GameLib.Gameplay;

namespace Sandbox
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class EditorWindow : Window
    {
        public EditorWindow()
        {
            InitializeComponent();

			ModeTabs.SelectionChanged += new SelectionChangedEventHandler(ModeTabs_SelectionChanged);

			EditorMode[] modes = 
			{ 
				new TerrainMode(this),
				new RiverMode(this),
				new RefImageMode(this)
			};

			Modes = modes;

			CurrentMode = Modes[ModeTabs.SelectedIndex];

			// create default map scene
			Play.New("../../Content/Data/Plays/Ragnarok/Ragnarok.xml;Play");

			m_MapScene = new MapScene(MapWindow);

			// use "new map" menu to create new one, now load default
			if (Core.Play.World.Maps.Count > 0)
			{
				m_Map = Core.Play.World.Maps[0];
			}
			else
			{
				m_Map = new Map();
				m_Map.Init(128, 128);
				Core.Play.World.Maps.Add(m_Map);
			}


			m_MapScene.Map = m_Map;	
		
			// add terrain to the box
			foreach (Terrain terrain in Core.Play.World.Terrains)
			{
				var boxItem = new ListBoxItem();
				boxItem.Content = terrain;
				TerrainPanel.ListBoxTerrain.Items.Add(boxItem);
			}

		}

		void ModeTabs_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			CurrentMode = Modes[ModeTabs.SelectedIndex];
		}

		private void Button_Click(object sender, RoutedEventArgs e)
		{
			//Sprite test = GenericSerializer.Open<Sprite>("Content/EntityTypes/SpriteA.xml");

			TileSets.Test();

		}

		private void MapWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			CurrentMode.MapWindow_MouseLeftButtonDown(e);
		}

		private void MapWindow_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			CurrentMode.MapWindow_MouseLeftButtonUp(e);
		}

		private void MapWindow_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
		{
			if (!m_IsDraggingMap)
			{
				m_IsDraggingMap = true;
				m_LastDraggingPoint = e.GetPosition(MapWindow);
				MapWindow_LoseFocus(e);
			}
		}

		private void MapWindow_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
		{
			if (m_IsDraggingMap)
			{
				m_IsDraggingMap = false;
				MapWindow_GainFocus(e);
			}			
		}

		private void MapWindow_MouseMove(object sender, MouseEventArgs e)
		{
			if (m_IsDraggingMap)
			{
				Point new_point = e.GetPosition(MapWindow);
				m_MapScene.Scroll(m_LastDraggingPoint.X - new_point.X, m_LastDraggingPoint.Y - new_point.Y);
				m_LastDraggingPoint = new_point;
			}
			else
			{
				CurrentMode.MapWindow_MouseMove(e);
			}
		}

		private void MapWindow_MouseLeave(object sender, MouseEventArgs e)
		{
			if (m_IsDraggingMap)
			{
				m_IsDraggingMap = false;
			}
			else
			{
				MapWindow_LoseFocus(e);
			}
		}

		private void MapWindow_GainFocus(MouseEventArgs e)
		{
			CurrentMode.MapWindow_GainFocus(e);
		}

		private void MapWindow_LoseFocus(MouseEventArgs e)
		{
			CurrentMode.MapWindow_LoseFocus(e);
		}


		private void MapWindow_MouseWheel(object sender, MouseWheelEventArgs e)
		{
			CurrentMode.MapWindow_MouseWheel(e);
		}


		//public TerrainMode TerrainMode { get; private set; }
		//public RiverMode RiverMode { get; private set; }
		public EditorMode[] Modes { get; private set; }

		public EditorMode CurrentMode
		{
			get { return m_CurrentMode; }
			set
			{
				if (m_CurrentMode != value && value != null)
				{
					if (m_CurrentMode != null)
						m_CurrentMode.End();

					m_CurrentMode = value;

					m_CurrentMode.Begin();
				}
			}
		}

		public MapScene MapScene
		{
			get { return m_MapScene; }
		}

		private void MenuItemSave_Click(object sender, RoutedEventArgs e)
		{
			Core.DataHandler.Write(m_Map, "../../Content/Data/Plays/Ragnarok/Maps/Earth.xml");
		}
		

		private Map m_Map;
		private MapScene m_MapScene;
		private bool m_IsDraggingMap;
		private Point m_LastDraggingPoint;
		private EditorMode m_CurrentMode;



    }
}
